package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;


import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

//年度对账单
@Controller
@RequestMapping("/nddzdcontroller")
public class nddzdcontroller {

	private static final Logger logger = Logger.getLogger(nddzdcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	 

	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response,HttpSession session, String limit ,String id,String n) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Calendar cal = Calendar.getInstance();
		OpenKey opk=new OpenKey();
		int year = cal.get(Calendar.YEAR);
		Map reqMap = new HashMap();
		reqMap.put("AUTHORITY", "YBGJJ147");
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		String NIAN=year+"";
		
		if(n==null||n.equals("")){
			b.setNIAN(NIAN);
			b.setGRZH(sessionIn.getGRZH());
			reqMap.put("BODY", b);
		}else{
			b.setNIAN(n);
			b.setGRZH(sessionIn.getGRZH());
			reqMap.put("BODY", b);
		}
		String result=opk.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		Map resMap=JSONUtil.toMap(JSONUtil.ToJsongString2(retMap.get("body")));
		
    	List retlist = (List)resMap.get("RETDATA");
		
			Map map =(Map) retlist.get(0);
			map.put("BNJCJE", resMap.get("BNJCJE").toString());
			map.put("HTDKJE", resMap.get("HTDKJE").toString());

			map.put("SYBJ", resMap.get("SYBJ").toString());

			map.put("TQJE", resMap.get("TQJE").toString());
			map.put("TQCS", resMap.get("TQCS").toString());
            

		   
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(retMap.get("RETURNCODE").equals("100")){
				//List resList = JSONUtil.toList(resMap.get("body").toString());
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(retMap.get("body")));
				//map.put("count", resList.size());
				map.put("code", 0);
				map.put("msg", null);
				mx.put("GRZH", sessionIn.getGRZH());
				map.put("data",mx);
				//list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("data","");
				map.put("CW", retMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
	
		return null;
	}
	
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(HttpServletRequest request, HttpServletResponse response,HttpSession session, String limit ,String id,String start,String end,String ny) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Calendar a=Calendar.getInstance();
		BODY b=new BODY();
		OpenKey p=new OpenKey();
		Map reqMap = new HashMap();
		reqMap.put("AUTHORITY", "YBGJJ147");
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setGRZH(sessionIn.getGRZH());
		
		if(ny==null||ny.equals("")){
			b.setNIAN(a.get(Calendar.YEAR)+"");
		}else{
			b.setNIAN(ny);
		}
		reqMap.put("BODY",b);
		String  result = p.getOpenKey(reqMap, session);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				mx.put("GRZH",sessionIn.getGRZH());
				map.put("code", 0);
				map.put("msg", null);
				map.put("data", mx);
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
	
		return null;
	}
	
	
}

